package com.bear.plan.jdbc.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.bear.PlatFormDao.plan.PlanDao;
import com.bear.model.entity.plan.Plan;

@Repository
public class PlanDaoImp implements PlanDao{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Plan> getAllPlans(int userid) {
		String sqlStr = "select id,detail,userid,percent " +
				" from plan where userid=?";
		final List<Plan> plans = new ArrayList<Plan>();
		jdbcTemplate.query(sqlStr,new Object[]{userid},
				new RowCallbackHandler() {
					@Override
					public void processRow(ResultSet rs) throws SQLException {
							Plan plan = new Plan();
							plan.setId(rs.getInt("id"));
							plan.setDetail(rs.getString("detail"));
							plan.setUserid(rs.getInt("userid"));
							plan.setPercent(rs.getInt("percent"));
							plans.add(plan);
					}
				});
		return plans;
	}

	@Override
	public void addPlan(Plan p) {
		String sqlStr = "insert into plan (detail,userid,percent)" +
				" values(?,?,?) ";
		Object[] args= {p.getDetail(),p.getUserid(),p.getPercent()};
		jdbcTemplate.update(sqlStr,args);
		
	}

}
